<!--
 * @Descripttion: 分类列表
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 17:34:08
-->
<template>
  <div>
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          @click="addEditActivifyClassify('add')"
          icon="el-icon-plus"
          >添加活动分类</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="title" label="分类名"> </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column prop="status" label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateClassify(scope.row.id, scope.row.status)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="
                  addEditActivifyClassify('edit', {
                    id: scope.row.id,
                    title: scope.row.title,
                    top: scope.row.top
                  })
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="page"
        :pageSize="limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
      <el-dialog
        :title="dialogForm.id ? '编辑分类' : '新增分类'"
        :visible.sync="centerDialogVisible"
        width="400px"
        center
      >
        <el-form
          @submit.native.prevent
          :model="dialogForm"
          :rules="dialogFormRules"
          ref="dialogForm"
          label-width="80px"
        >
          <el-form-item label="分类名" prop="title">
            <el-input
              v-model="dialogForm.title"
              placeholder="请输入分类名"
            ></el-input>
          </el-form-item>
          <el-form-item label="排序值" prop="top">
            <el-input
              v-model.number="dialogForm.top"
              placeholder="请输入排序值"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">{{
            $t('action.cancel')
          }}</el-button>
          <el-button type="primary" @click="submitFormInfo('dialogForm')">{{
            $t('action.comfirm')
          }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      centerDialogVisible: false,
      dialogForm: {
        title: '',
        top: 0,
        id: ''
      },
      dialogFormRules: {
        title: { required: true, type: 'string', message: '请输入标题', trigger: 'blur' },
        top: { required: true, type: 'number', message: '请输入排序值', trigger: 'blur' }
      }
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let { page, limit } = this
      let obj = { page, page_count: limit }
      this.$api.getActivityClassifyList(obj).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let { dialogForm } = this
          let methodModel = dialogForm.id ? 'editActivityClassifyInfo' : 'addActivityClassifyInfo'
          this.$api[methodModel](dialogForm).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t(dialogForm.id ? 'tips.successRev' : 'tips.successSave'))
              this.centerDialogVisible = false
              this.getTableDataList()
            }
          })
        }
      })
    },
    addEditActivifyClassify (type, formInfo) {
      let obj = {
        title: '',
        top: 0,
        id: ''
      }
      this.dialogForm = type === 'add' ? obj : formInfo
      this.centerDialogVisible = true
    },
    // 上下架、删除
    updateClassify (id, status) {
      this.$api.updateActivityClassifyInfo({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
            this.getTableDataList()
          }
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    // 删除
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateClassify(id, -1)
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
